import { defineStore } from 'pinia'
import {
  postUserList,
  deleteUser,
  createUser,
  updateUser
} from '@/service/main/system/system'

interface ISystemState {
  usersList: any[]
  usersTotalCount: number
}
export const useSystemStore = defineStore('system', {
  state: (): ISystemState => ({
    usersList: [],
    usersTotalCount: 0
  }),
  actions: {
    async postUsersListAction(queryInfo: any) {
      //发送list请求
      const userListResult = await postUserList(queryInfo)
      const { totalCount, list } = userListResult.data
      this.usersTotalCount = totalCount
      this.usersList = list
    },
    // 删除用户
    async postUsersDeleteAction(userId: number) {
      const userDeleteResult = await deleteUser(userId)
      this.postUsersListAction({ offset: 0, size: 10 })
      console.log(userDeleteResult)
    },
    // 添加用户
    async postUsersAddAction(userInfo: any) {
      const newResult = await createUser(userInfo)
      this.postUsersListAction({ offset: 0, size: 10 })
      console.log(newResult)
    },
    // 修改用户信息
    async patchUsersEditAction(userInfo: any, userId: number) {
      const editResult = await updateUser(userInfo, userId)
      this.postUsersListAction({ offset: 0, size: 10 })
    }
  }
})
